#!/usr/local/bin/python
#-*- encoding:utf-8 -*- 
import codecs 
from whoosh.index import create_in  
from whoosh.fields import *  
from chinesetokenizer import ChineseAnalyzer
from whoosh import qparser
import ConfigParser

from whoosh.collectors import *
from whoosh.index import open_dir


class MyCollector(TopCollector):
    def __init__(self, limit=10, usequality=True, **kwargs):
        TopCollector.__init__(self, limit=10, usequality=True, **kwargs);
        self.titleScore={};
        
    def collect(self, sub_docnum):
        global_docnum = self.offset  + sub_docnum;
        
        score = self.matcher.score()

        if self.final_fn:
            score = self.final_fn(self.top_searcher, global_docnum, score);

        ts = 0.0;
        for tm in self.matcher.term_matchers():
            if tm.term()[0] == 'content':
                ts += tm.score();

        if self.final_fn:
            print 'CAREFUL';
            ts = self.final_fn(self.top_searcher, global_docnum, score)
        self.titleScore[global_docnum] = ts;

        return self._collect(global_docnum, score);

config = ConfigParser.ConfigParser();
config.read('config');

def toFinalResults(queryString, queryID, results):
    for hit in results:
        docnum = hit.docnum;
        #ts = titleScore[docnum];
        s = '%s\t%s\t%s\t%f\t%s' %(queryID, queryString.decode('utf-8'), hit.fields()['appid'], hit.score, hit.fields()['content'].strip());
        print s.encode('utf-8');


ix = open_dir(config.get('bm25', 'MulFieldsIndex'));

parser = qparser.MultifieldParser(['content', 'description'], ix.schema, fieldboosts={'content':1.00, 'description':0.0000000001}, group=qparser.syntax.OrGroup);

with open(config.get('bm25', 'TestQuery'), 'r') as f:
    with ix.searcher() as searcher:
        i = 0;
        for line in f:
            li = line.strip().split('|', 1);
            queryString = li[0];
            i += 1;
            queryID = str(i);
            #tc = MyCollector(limit =200);
            query = parser.parse(queryString.decode('utf-8'));
            results = searcher.search(query, limit=None);
            #results = tc.results();
            #titleScore = tc.titleScore;
            if 0 != len(results):
                toFinalResults(queryString, queryID, results);
